package cn.example.web.interceptor;

import cn.example.web.base.AbstractController;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import org.artifact.core.constant.ArtifactErrorCode;
import org.artifact.core.exception.AbstractArtifactException;

/**
 * 异常拦截器
 * @author apple
 *
 */
public class ExceptionInterceptor implements Interceptor {
	static Log log = LogFactory.get(ExceptionInterceptor.class);

	public void intercept(Invocation inv) {
		try {
			inv.invoke();
		} catch (Exception e) {
			AbstractController controller = (AbstractController) inv.getController();
			if (e instanceof AbstractArtifactException) {
				AbstractArtifactException le = (AbstractArtifactException) e;
				controller.renderError(le.getCode(), le.getMessage());
			} else {
				e.printStackTrace();
				log.error(e);
				controller.renderError(ArtifactErrorCode.UNKNOWN_ERROR, "服务器错误");
			}
		}
	}
}